﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StyleMVVM.DotNet.Aspects.Impl
{
	public class AspectInvokeContext : IAspectInvokeContext
	{
		private int proceedIndex;
		private readonly IAspect[] aspects;
		private readonly Action<IAspectInvokeContext> invokeBaseAction;

		public AspectInvokeContext(string memberName,
		                           object target,
		                           object[] args,
		                           IAspect[] aspects,
		                           Action<IAspectInvokeContext> invokeBaseAction)
		{
			MemberName = memberName;
			Target = target;
			Args = args;

			this.aspects = aspects;
			this.invokeBaseAction = invokeBaseAction;
		}

		public string MemberName { get; private set; }

		public object Target { get; private set; }

		public object[] Args { get; private set; }

		public object ReturnValue { get; set; }

		public void Proceed()
		{
			if (proceedIndex < aspects.Length)
			{
				IAspect aspect = aspects[proceedIndex];

				proceedIndex++;

				aspect.Invoke(this);
			}
			else if (proceedIndex == aspects.Length)
			{
				proceedIndex++;

				invokeBaseAction(this);
			}
		}
	}
}
